import Mock, { mock } from "mockjs";
// var Mock = require('mockjs')
var Random = Mock.Random;

//道路站点
const data = Mock.mock({
  "data|20": [
    {
      "id|+1": 0, //商品id
      "road|1": ["教育路", "创业路", "新华路", "华北路"],
      "nickname|1": [
        "教育路停车场",
        "创业路停车场",
        "新华路停车场",
        "华北路停车场",
      ],
      token: mock('@string("number", 5)'),
      kind11: "@csentence(6)",
      desc: Random.cparagraph(),
      "nums|1-100": 100,
      "price|1-20": 20,
      "state|1": ["0", "1"],
      "parkstate|1": ["路边停车场", "室内停车场"],
      time: '@datetime("2022-MM-dd A HH:mm:ss")',
    },
  ],
});

// prolist  get
Mock.mock("/parking", "get", () => {
  return data;
});

//添加功能
Mock.mock("/parking/add", "post", (comfig) => {
  const data1 = JSON.parse(comfig.body);
  data1.id = data.data.length + 1;
  data.data.push(data1);
  return data;
});
//查询功能

Mock.mock("/parking/search", "post", (comfig) => {
  const data1 = JSON.parse(comfig.body);
  console.log(data1);
  const data2 = data.data.filter((item) => {
    return (
      item.kind11 === data1.kin11 ||
      item.nickname === data1.nickname ||
      item.parkstate === data1.parkstate ||
      item.road === data1.road ||
      item.token === data1.token ||
      item.state === data1.state
    );
  });
  return data2;
});
//删除功能
Mock.mock("/parking/remove", "post", (comfig) => {
  const data1 = JSON.parse(comfig.body);
  data.data.forEach((item, index) => {
    if (item.id === data1.id) {
      data.data.splice(index, 1);
    }
  });
  return data;
});
///parking/set 设置当前数据
Mock.mock("/parking/set", "post", (comfig) => {
  const data1 = JSON.parse(comfig.body);
  console.log(data1);
  data.data.forEach((item, index) => {
    if (item.id === data1.id) {
      console.log(index);
      data.data.splice(index, 1, data1);
    }
  });

  return data;
});
///parking/set 查看当前数据
Mock.mock("/parking/wacth", "post", (comfig) => {
  const data1 = JSON.parse(comfig.body);
  console.log(data1);

  data.data.map((item) => {
    return item.id === data1.id;
  });
});
